package com.iking.housekeeping.controller.export;

import com.iking.housekeeping.common.CodeEnum;
import com.iking.housekeeping.common.ReturnCode;
import com.iking.housekeeping.httpbean.servicerecord.SelectServiceRecord;
import com.iking.housekeeping.httpbean.servicestaff.reqbean.StaffBySelect;
import com.iking.housekeeping.service.export.ExportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;

/**
 * @Author root
 * @Date 2018/5/18
 * @Describe 文件导出
 * 目前导出文档有三种
 * 1、服务记录
 * 2、服务人员
 * 3、老人
 */
@RestController
@RequestMapping("/export")
public class ExportController {

    @Autowired
    private ExportService exportService;

    /**
     * 根据类型
     */
    @PostMapping("/servicerecord")
    public ReturnCode getAllMenu(@RequestBody SelectServiceRecord selectServiceRecord, javax.servlet.http.HttpServletRequest request) {
        String filePath=exportService.exportRecorder(selectServiceRecord,request);
        //判断文件是否生成
        File f=new File(filePath);
        if(f.exists())
        {
            String name = f.getName();
            return new ReturnCode(CodeEnum.SUCCESS,"成功",name);
        }
        return new ReturnCode(CodeEnum.FAIL,"生成文件失败！");
    }
    /**
     * 根据人员导出老人信息
     */
    @PostMapping("/exportServiceElderly")
    public ReturnCode exportServiceElderly (@RequestBody StaffBySelect selectServiceRecord, javax.servlet.http.HttpServletRequest request){
        String filePath=exportService.exportServiceElderly(selectServiceRecord,request);
        //判断文件是否生成
        File f=new File(filePath);
            if(f.exists())
        {
            String name = f.getName();
            return new ReturnCode(CodeEnum.SUCCESS,"成功",name);
        }
        return new ReturnCode(CodeEnum.FAIL,"生成文件失败！");
    }

    /**
     * 导出老人台账信息
     */
    @PostMapping("/exportElderlyAccountInfo")
    public ReturnCode exportElderlyAccountInfo (@RequestBody SelectServiceRecord selectServiceRecord, javax.servlet.http.HttpServletRequest request){
        String filePath=exportService.exportElderlyAccountInfo(selectServiceRecord,request);
        //判断文件是否生成
        File f=new File(filePath);
        if(f.exists())
        {
            String name = f.getName();
            return new ReturnCode(CodeEnum.SUCCESS,"成功",name);
        }
        return new ReturnCode(CodeEnum.FAIL,"生成文件失败！");
    }
    /**
     * 文件下载 解决文件中文乱码问题
     * 分布式 系统中 必须与Controller 在一起
     *
     * @param response fwq
     */
    @GetMapping("/downloadFile/{filePath}/{fileName}/{fileSuffix}")
    public  void downloadFile(@PathVariable("filePath") String filePath, @PathVariable("fileName")String fileName,@PathVariable("fileSuffix")String fileSuffix,
                              HttpServletRequest request, HttpServletResponse response) {
        OutputStream out = null;
        InputStream in = null;
        try {
            String url = exportService.filePath(request) + filePath;
            //设置content-disposition响应头控制浏览器以下载的形式打开文件
            // 中文文件名要使用URLEncoder.encode方法进行编码，否则会出现文件名乱码
            String name=fileName+"."+fileSuffix;
            response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(name, "UTF-8"));
            in = new FileInputStream(url);//获取文件输入流
            int len = 0;
            byte[] buffer = new byte[8 * 1024]; //8kb 缓冲区
            out = response.getOutputStream();
            while ((len = in.read(buffer)) > 0) {
                out.write(buffer, 0, len); //将缓冲区的数据输出到客户端浏览器
            }
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                in.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

}
